package vip.web3.api.constans.enums;

import org.apache.commons.lang3.StringUtils;
import vip.web3.api.form.user.EditForm;
import vip.web3.common.enums.GenderEnum;
import vip.web3.common.exception.RRException;
import vip.web3.common.func.Checker;

import java.util.Objects;

/**
 * aut-horlilei
 */

public enum UserEditEnum {

    /**
     * 昵称
     */
    NICK(1, "昵称") {
        @Override
        public void valid(EditForm form) {
            if (StringUtils.isBlank(form.getNick())) {
                throw new RRException("昵称不能空!", -1);
            }
        }
    },

    /**
     * 性别
     */
    GENDER(2, "性别") {
        @Override
        public void valid(EditForm form) {
            if (Objects.isNull(form.getGender())) {
                throw new RRException("性别不能空!", -1);
            }
            if (Objects.isNull(GenderEnum.get(form.getGender()))) {
                throw new RRException("性别只能是1,2,3!", -1);
            }
        }
    },

    /**
     * 头像
     */
    HEAD(3, "头像") {
        @Override
        public void valid(EditForm form) {
            if (StringUtils.isBlank(form.getNick())) {
                throw new RRException("头像不能空!", -1);
            }
        }
    },
    ;

    private final Integer code;
    private final String desc;

    UserEditEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static UserEditEnum get(Integer code) {
        for (UserEditEnum value : values()) {
            if (value.code.equals(code)) {
                return value;
            }
        }
        return null;
    }

    public abstract void valid(EditForm form);

    public void check(Checker checker) {
        if (Objects.nonNull(checker)) {
            checker.check();
        }
    }

    public Integer getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }
}
